<?php
require_once "../lib/adodb5/adodb.inc.php";
require_once "../src/webapp/db/ORM.php";

$conn = new db\Connection('sqlite://test.db');

db\Model::$connection = $conn;
db\Model::$autoDDL = true;

class Person extends db\Model
{
	protected static $fields = array(
		'name' => 'string(30)',
		'gender' => 'char(1)',
		'age' => 'integer(3)'
	);

	protected static $indexes = array(
		'sid' => 'unique',
		'gender, age' => 'index'
	);
}

echo(microtime() . '----------<br/>');
// inserting 100 person in transaction
$conn->BeginTrans();
for ($i = 0; $i < 100; $i++) {
	$p = new Person();
	$p->name = 'person ' . $i;
	$p->gender = $i % 2 ? 'M' : 'F';
	$p->age = rand(10, 50);
	$p->put();
}
$conn->CommitTrans();

echo(microtime() . '----------<br/>');
// find male people whom older than 30 
$people = Person::all()->filter('gender =', 'M')->filter('age >', 30)->order('-name');

echo(microtime() . '----------<br/>');

// count found people
echo count($people);

// iterate found people
foreach ($people as $person) {
	echo $person->name . '<br/>';
}
echo(microtime() . '----------<br/>');
echo "\n\n";

?>